package com.blogs.tools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;


public class HtmlFormat {
	public static String FormatContent(Context context, String html){
		//是否图片模式
		html=HtmlFormat.FormatLink(html);//格式化连接标签
		DBSharedPreferences sp=new DBSharedPreferences(context);
		boolean isImgMode=sp.GetIsShowPic();
		
		if(!isImgMode){//不是图片浏览模式
			html=HtmlFormat.ReplaceImgTag(html);//无图片浏览模式下格式化图片标签
			html=HtmlFormat.ReplaceVideoTag(html);//格式化视频标签
		}
		else
		{
			html=HtmlFormat.FormatImgTag(html);//图片浏览模式下格式化图片标签
		}
		html=HtmlFormat.ReplaceStyle(html);
		return html;
	}
	/**
	 * 过滤html特殊字符
	 * @param str
	 * @return
	 */
	public static String HtmlToText(String str){
		str=str.replace("<br />", "\n");
		str=str.replace("<br/>", "\n");
		str=str.replace("&nbsp;&nbsp;", "\t");
		str=str.replace("&nbsp;", " ");
		str=str.replace("&#39;","\\");
		str=str.replace("&quot;", "\\");
		str=str.replace("&gt;",">");
		str=str.replace("&lt;","<");
		str=str.replace("&amp;", "&");
		
		return str;
	}
	static final Pattern patternHtml=Pattern.compile("<.+?>");
	/**
	 * 移除html标记
	 * @param html
	 * @return
	 */
	public static String RemoveHtmlTag(String html){
		Matcher m=patternHtml.matcher(html);
		while(m.find()){
			html= m.replaceAll("");
		}
		return html;
	}
	
	/**
	 * 判断是否含有图片内容
	 * @param html
	 * @return
	 */
	static final Pattern formatLink=Pattern.compile("<a(.+?)href=\"(.+?)\"(.+?)>(.+?)</a>");
	public static String FormatLink(String html){
		Matcher m=formatLink.matcher(html);
		while(m.find()){
			//html= m.replaceAll("【<a href=\"$2\">点击查看图片</a>】");
			html= m.replaceAll("<a href=\"#\" onClick=\"window.cnblogs.openURL('$2')\">$4</a>");
		}
		return html;
	}
	
	/**
	 * 判断是否含有图片内容
	 * @param html
	 * @return
	 */
	static final Pattern patternImg=Pattern.compile("<img(.+?)src=\"(.+?)\"(.+?)(onload=\"(.+?)\")?([^\"]+?)>");
	public static boolean IsContainImg(String html){
		Matcher m=patternImg.matcher(html);
		while(m.find()){
			return true;
		}
		return false;
	}
	/**
	 * 移除图片标记
	 * @param html
	 * @return
	 */
	public static String RemoveImgTag(String html){
		Matcher m=patternImg.matcher(html);
		while(m.find()){
			html= m.replaceAll("");
		}
		return html;
	}
	/**
	 * 替换图片标记
	 * @param html
	 * @return
	 */
	static final Pattern patternImgSrc=Pattern.compile("<img(.+?)src=\"(.+?)\"(.+?)>");
	public static String ReplaceImgTag(String html){
		Matcher m=patternImgSrc.matcher(html);
		while(m.find()){
			//html= m.replaceAll("【<a href=\"$2\">点击查看图片</a>】");
			html= m.replaceAll("【<a href=\"#\" onClick=\"window.cnblogs.showImg('$2')\">点击查看图片</a>】");
		}
		return html;
	}
	/**
	 * 格式化图片标记
	 * @param html
	 * @return
	 */
	public static String FormatImgTag(String html){
		Matcher m=patternImgSrc.matcher(html);
		while(m.find()){
			//html= m.replaceAll("【<a href=\"$2\">点击查看图片</a>】");
			html= m.replaceAll("<img src=\"$2\" onClick=\"window.cnblogs.showImg('$2')\" >" );
		}
		return html;
	}
	
	/**
	 * 移除视频标记
	 */
	static final Pattern patternVideo=Pattern.compile("<object(.+?)>(.*?)<param name=\"src\" value=\"(.+?)\"(.+?)>(.+?)</object>");
	public static String RemoveVideoTag(String html){
		Matcher m=patternVideo.matcher(html);
		while(m.find()){
			html= m.replaceAll("");
		}
		return html;
	}
	/**
	 * 替换视频标记
	 */
	static final Pattern patternVideoSrc=Pattern.compile("<object(.+?)>(.*?)<param name=\"src\" value=\"(.+?)\"(.+?)>(.+?)</object>");
	public static String ReplaceVideoTag(String html){
		Matcher m=patternVideoSrc.matcher(html);
		while(m.find()){
			html= m.replaceAll("【<a href=\"$3\">点击查看视频</a>】");
		}
		return html;
	}
	/**
	 * 过滤font-size标记
	 * */
	static final Pattern patternFontStyle=Pattern.compile("font-size:(.+?)pt");
	static final Pattern patternFontStylePx=Pattern.compile("font-size:(\\s?.+?)px;");
	public static String ReplaceStyle(String html){
		Matcher m=patternFontStyle.matcher(html);
		while(m.find()){
			html= m.replaceAll("");
		}
		Matcher mpx=patternFontStylePx.matcher(html);
		while(mpx.find())
		{
			html=mpx.replaceAll("");
		}
		html=html.replace("font-size: x-large;", "");
		return html;
	}
	
	/**
	 * 匹配评论@标记
	 */
	static final Pattern patternAt=Pattern.compile("<a href=\"(.+?)\" title=\"(.+?)\">@</a>(.+?)<br/>");
	static final Pattern patternAt2=Pattern.compile("@(.+?)<br/>");
	static final Pattern patternRef=Pattern.compile("<fieldset class=\"comment_quote\"><legend><a href=\"(.+?)\" title=\"(.+?)\">引用</a></legend>(.+?)</fieldset><br/>");
	static final Pattern patternRef2=Pattern.compile("<fieldset class=\"comment_quote\"><legend>引用</legend>@(.+?)<br/>(.+?)</fieldset><br/>");
	public static Spanned ReplaceAt(String html){
		Matcher mr=patternRef.matcher(html);
		while(mr.find()){
			html= mr.replaceAll("<font color='#ff9933'>引用  [$3]</font>\n<br/>");
		}
		Matcher mr2=patternRef2.matcher(html);
		while(mr2.find()){
			html= mr2.replaceAll("<font color='#ff9933'>引用  [$2]</font>\n<br/>");
		}
		Matcher m=patternAt.matcher(html);
		while(m.find()){
			html= m.replaceAll("<font color='#0273fe'>@$3</font>\n<br/>");
		}
		Matcher m2=patternAt2.matcher(html);
		while(m2.find()){
			html= m2.replaceAll("<font color='#0273fe'>@$1</font>\n<br/>");
		}

		return Html.fromHtml(html);
	}
	/**
	 * 替换图片标记
	 * @param html
	 * @return
	 */
	static final Pattern patternIngAt=Pattern.compile("@(.+?)：");
	public static String FormatIngAt(String html){
		Matcher m=patternIngAt.matcher(html);
		while(m.find()){
			html= m.replaceAll("<font color=blue >@$1</font>：");
		}
		return html;
	}
}
